#include <sys_config.h>
#include <types.h>
#include <basic_types.h>
#include <mediatypes.h>
#include <api/libc/printf.h>
#include <api/libc/string.h>
#include <osal/osal.h>

#include <api/libosd/osd_lib.h>

#include "osdobjs_def.h"

#include "string.id"
#include "images.id"
#include "osd_config.h"
#include "osd_rsc.h"
#include "menus_root.h"

#include "copper_common/system_data.h"
#include "copper_common/menu_api.h"
#include "win_com_menu_define.h"
#include "win_com.h"

/*******************************************************************************
*	Objects definition
*******************************************************************************/
extern CONTAINER win_lan_con;

extern CONTAINER lan_item_con1;
extern CONTAINER lan_item_con2;
extern CONTAINER lan_item_con3;
extern CONTAINER lan_item_con4;
extern CONTAINER lan_item_con5;
#ifdef SHOW_WELCOME_SCREEN
extern CONTAINER lan_item_con6;
#endif

extern TEXT_FIELD lan_item_txtname1;
extern TEXT_FIELD lan_item_txtname2;
extern TEXT_FIELD lan_item_txtname3;
extern TEXT_FIELD lan_item_txtname4;
extern TEXT_FIELD lan_item_txtname5;
#ifdef SHOW_WELCOME_SCREEN
extern TEXT_FIELD lan_item_txtname6;
#endif


extern TEXT_FIELD lan_item_txtset1;
extern TEXT_FIELD lan_item_txtset2;
extern TEXT_FIELD lan_item_txtset3;
extern TEXT_FIELD lan_item_txtset4;
extern TEXT_FIELD lan_item_txtset5;

extern TEXT_FIELD lan_item_line1;
extern TEXT_FIELD lan_item_line2;
extern TEXT_FIELD lan_item_line3;
extern TEXT_FIELD lan_item_line4;
extern TEXT_FIELD lan_item_line5;

static VACTION lan_item_con_keymap(POBJECT_HEAD pObj, UINT32 key);
static PRESULT lan_item_con_callback(POBJECT_HEAD pObj, VEVENT event, UINT32 param1, UINT32 param2);

static VACTION lan_con_keymap(POBJECT_HEAD pObj, UINT32 key);
static PRESULT lan_con_callback(POBJECT_HEAD pObj, VEVENT event, UINT32 param1, UINT32 param2);
UINT8  win_language_get_lang_idx(UINT8 uLang);

#define BTN_SH_IDX   WSTL_BUTTON_SHOR_HD
#define BTN_HL_IDX   WSTL_BUTTON_SHOR_HI_HD
#define BTN_SL_IDX   WSTL_BUTTON_01_FG_HD
#define BTN_GRY_IDX  WSTL_BUTTON_SHOR_GRY_HD

#ifndef SD_UI
#define BTN_W		160
#define BTN_H		44

//the main window frame
#define	MAIN_W_L     	74//210 - 136
#define	MAIN_W_T     	98//138 - 40
#ifdef SUPPORT_CAS_A
#define	MAIN_W_W		886
#else
#define	MAIN_W_W		866
#endif
#define	MAIN_W_H     	488
#else
#define BTN_W		120
#define BTN_H		32

//the main window frame
#define	MAIN_W_L     	17//210 - 136
#define	MAIN_W_T     	57//138 - 40
#define	MAIN_W_W		570
#define	MAIN_W_H     	370

#endif

#define LDEF_CON(root, varCon,nxtObj,ID,IDl,IDr,IDu,IDd,l,t,w,h,conobj,focusID)		\
    DEF_CONTAINER(varCon,root,nxtObj,C_ATTR_ACTIVE,0, \
    ID,IDl,IDr,IDu,IDd, l,t,w,h, CON_SH_IDX,CON_HL_IDX,CON_SL_IDX,CON_GRY_IDX,   \
    lan_item_con_keymap,lan_item_con_callback,  \
    conobj, focusID,1)

#define LDEF_TXTNAME(root,varTxt,nxtObj,ID,IDl,IDr,IDu,IDd,l,t,w,h,resID,str)		\
    DEF_TEXTFIELD(varTxt,root,nxtObj,C_ATTR_ACTIVE,0, \
    ID,IDl,IDr,IDu,IDd, l,t,w,h, TXTN_SH_IDX,TXTN_HL_IDX,TXTN_SL_IDX,TXTN_GRY_IDX,   \
    NULL,NULL,  \
    C_ALIGN_LEFT | C_ALIGN_VCENTER, 0,0,resID,str)

#define LDEF_TXTSET(root,varTxt,nxtObj,ID,IDl,IDr,IDu,IDd,l,t,w,h,resID,str)		\
    DEF_TEXTFIELD(varTxt,root,nxtObj,C_ATTR_ACTIVE,0, \
    ID,IDl,IDr,IDu,IDd, l,t,w,h, TXTS_SH_IDX,TXTS_HL_IDX,TXTS_SL_IDX,TXTS_GRY_IDX,   \
    NULL,NULL,  \
    C_ALIGN_CENTER| C_ALIGN_VCENTER, 20,0,resID,str)


#define LDEF_MM_ITEM(root,varCon,nxtObj,varTxtName,varTxtset,varLine,ID,IDu,IDd,l,t,w,h,nameID,setstr)	\
	LDEF_CON(&root,varCon,nxtObj,ID,ID,ID,IDu,IDd,l,t,w,h,&varTxtName,1)	\
	LDEF_TXTNAME(&varCon,varTxtName,&varTxtset,0,0,0,0,0,l + TXTN_L_OF, t + TXTN_T_OF,TXTN_W,TXTN_H,nameID,NULL)	\
	LDEF_TXTSET(&varCon,varTxtset,  NULL/*&varLine*/        ,1,1,1,1,1,l + TXTS_L_OF, t + TXTS_T_OF,TXTS_W,TXTS_H,0,setstr)\
	LDEF_LINE(&varCon,varLine,NULL,1,1,1,1,1,l + LINE_L_OF, t + LINE_T_OF,LINE_W,LINE_H,0,0)

#ifdef SHOW_WELCOME_SCREEN
  #define LDEF_TXT_BUTTON(root,varTxt,nxtObj,ID,l,t,w,h,nameID)		\
    DEF_TEXTFIELD(varTxt,root,nxtObj,C_ATTR_ACTIVE,0, \
    ID,0,0,0,0, l,t,w,h, BTN_SH_IDX,BTN_HL_IDX,BTN_SL_IDX,BTN_GRY_IDX,   \
    NULL,NULL,  \
    C_ALIGN_CENTER | C_ALIGN_VCENTER, 0,0,nameID,NULL)


  #define LDEF_MENU_ITEM_TXT(root,varCon,nxtObj,varTxt,ID,IDu,IDd,l,t,w,h,nameID) \
    LDEF_CON(&root,varCon,nxtObj,ID,ID,ID,IDu,IDd,l,t,w,h,&varTxt,ID)   \
    LDEF_TXT_BUTTON(&varCon,varTxt,NULL,ID,l,t,w,h,nameID)
    
  LDEF_MM_ITEM(win_lan_con,lan_item_con1, &lan_item_con2,lan_item_txtname1,lan_item_txtset1,lan_item_line1,1,6,2,	\
				CON_L, CON_T + (CON_H + CON_GAP)*0,CON_W,CON_H,	RS_SYSTEM_LANGUAGE,display_strs[6])
#else
  LDEF_MM_ITEM(win_lan_con,lan_item_con1, &lan_item_con2,lan_item_txtname1,lan_item_txtset1,lan_item_line1,1,5,2,	\
				CON_L, CON_T + (CON_H + CON_GAP)*0,CON_W,CON_H,	RS_SYSTEM_OSD_LANGUAGE/*RS_SYSTEM_LANGUAGE*/,display_strs[0])
#endif

LDEF_MM_ITEM(win_lan_con,lan_item_con2, &lan_item_con3,lan_item_txtname2,  lan_item_txtset2,lan_item_line2,2,1,3,	\
				CON_L, CON_T + (CON_H + CON_GAP)*1,CON_W,CON_H,	RS_SYSTEM_LANGUAGE_FIRST_AUDIO,NULL)
LDEF_MM_ITEM(win_lan_con,lan_item_con3, &lan_item_con4,lan_item_txtname3,lan_item_txtset3,lan_item_line3,3,2,4,	\
				CON_L, CON_T + (CON_H + CON_GAP)*2,CON_W,CON_H,	RS_SYSTEM_LANGUAGE_SECOND_AUDIO,NULL)
LDEF_MM_ITEM(win_lan_con,lan_item_con4, &lan_item_con5,lan_item_txtname4,lan_item_txtset4,lan_item_line4,4,3,5,	\
				CON_L, CON_T + (CON_H + CON_GAP)*3,CON_W,CON_H,	RS_SYSTEM_LANGUAGE_SUBTITLE_LANGUAGE,NULL)
#ifdef SHOW_WELCOME_SCREEN
LDEF_MM_ITEM(win_lan_con,lan_item_con5, &lan_item_con6,lan_item_txtname5,lan_item_txtset5,lan_item_line5,5,4,6,	\
				CON_L, CON_T + (CON_H + CON_GAP)*4,CON_W,CON_H,	RS_SYSTEM_LANGUAGE_TELETEXT,NULL)


LDEF_MENU_ITEM_TXT(win_lan_con, lan_item_con6, NULL, lan_item_txtname6, 6, 5, 1, \
                   CON_W-40, CON_T + (CON_H + CON_GAP)*8, BTN_W, BTN_H, RS_DISPLAY_NEXT)
#else

				
LDEF_MM_ITEM(win_lan_con,lan_item_con5, NULL,lan_item_txtname5,lan_item_txtset5,lan_item_line5,5,4,1,	\
				CON_L, CON_T + (CON_H + CON_GAP)*4,CON_W,CON_H,	RS_SYSTEM_LANGUAGE_TELETEXT,NULL)
#endif				


DEF_CONTAINER(win_lan_con,NULL,NULL,C_ATTR_ACTIVE,0, \
    1,0,0,0,0, W_L,W_T,W_W,W_H, WIN_SH_IDX,WIN_HL_IDX,WIN_SL_IDX,WIN_GRY_IDX,   \
    lan_con_keymap,lan_con_callback,  \
    (POBJECT_HEAD)&lan_item_con1, 1,0)

UINT16 m_TitleStringId = RS_SYSTEM_LANGUAGE;//RS_SYSTEM_WELCOME;

/*******************************************************************************
*	Local functions & variables define
*******************************************************************************/
CONTAINER* lan_items[] =
{
	&lan_item_con1,
	&lan_item_con2,
	&lan_item_con3,
	&lan_item_con4,
	&lan_item_con5,
#ifdef SHOW_WELCOME_SCREEN
	&lan_item_con6,
#endif
};

POBJECT_HEAD lan_item_name[] =
{
	(POBJECT_HEAD)&lan_item_txtname1,
	(POBJECT_HEAD)&lan_item_txtname2,
	(POBJECT_HEAD)&lan_item_txtname3,
	(POBJECT_HEAD)&lan_item_txtname4,
	(POBJECT_HEAD)&lan_item_txtname5,
#ifdef SHOW_WELCOME_SCREEN
	(POBJECT_HEAD)&lan_item_txtname6,
#endif
};

POBJECT_HEAD lan_item_set[] =
{
	(POBJECT_HEAD)&lan_item_txtset1,
	(POBJECT_HEAD)&lan_item_txtset2,
	(POBJECT_HEAD)&lan_item_txtset3,
	(POBJECT_HEAD)&lan_item_txtset4,
	(POBJECT_HEAD)&lan_item_txtset5,
};


UINT16 stream_lang_str_ids[] = /* Should have the same order as stream_iso_639lang_abbr*/
{
    RS_LANGUAGE_ENGLISH,
#if(AOV_THAI_SUPPORT_ENABLE == SYS_FUNC_ON)
	RS_LANGUAGE_THAI,
#endif
#if(AOV_VIE_SUPPORT_ENABLE == SYS_FUNC_ON)
	RS_LANGUAGE_VIE,
#endif
#if(AOV_CHS_SUPPORT_ENABLE == SYS_FUNC_ON)
	RS_LANGUAGE_CHINA,
#endif
#if(AOV_MALAY_SUPPORT_ENABLE == SYS_FUNC_ON)
	RS_LANGUAGE_MALAYSIA,
#endif
#if(AOV_IND_SUPPORT_ENABLE == SYS_FUNC_ON)
	RS_LANGUAGE_INDONESIAN,
#endif
#if(AOV_ARABIC_SUPPORT_ENABLE == SYS_FUNC_ON)
    RS_LANGUAGE_ARIBIC,
#endif
#if(AOV_TAMIL_SUPPORT_ENABLE == SYS_FUNC_ON)
	RS_LANGUAGE_TAMIL,
#endif
#if(AOV_SPAN_SUPPORT_ENABLE == SYS_FUNC_ON) //Martin@20150225
	RS_LANGUAGE_SPANISH,
#endif
#if(AOV_POR_SUPPORT_ENABLE == SYS_FUNC_ON) //Martin@20150225
	RS_LANGUAGE_PORTUGUESE,
#endif
};
#define MENU_LANGUAGE_STREAM_NUMBER (sizeof(stream_lang_str_ids)/sizeof(stream_lang_str_ids[0]))

UINT8 get_language_stream_number(void)
{
	return MENU_LANGUAGE_STREAM_NUMBER;
}

UINT8 lang_eng[] 	= {0x00,0x45,0x00,0x6e,0x00,0x67,0x00,0x6c,0x00,0x69,0x00,0x73,0x00,0x68,0x00,0x00};/*English(Unicode)*/
#if(AOV_THAI_SUPPORT_ENABLE == SYS_FUNC_ON)
//UINT8 lang_thai[]	= {0x00,0x54,0x00,0x68,0x00,0x61,0x00,0x69,0x00,0x00};/*Thai(Unicode)*/
UINT8 lang_thai[]	= {0x0e,0x20, 0x0e,0x32, 0x0e,0x29, 0x0e,0x32, 0x0e,0x44, 0x0e,0x17, 0x0e,0x22, 0x00,0x00};
#endif
#if(AOV_VIE_SUPPORT_ENABLE == SYS_FUNC_ON)
//UINT8 lang_vie[]	= {0x00,0x56, 0x00,0x69, 0x00,0x65, 0x00,0x74, 0x00,0x6e, 0x00,0x61, 0x00,0x6d, 0x00,0x00};/*Vietnam(Unicode)*/
UINT8 lang_vie[]	= {0x00,0x56, 0x00,0x69, 0x00,0xea, 0x03,0x23, 0x00,0x74, 0x00,0x20, 0x00,0x6e, 0x00,0x61, 0x00,0x6d, 0x00,0x00};
#endif
#if(AOV_CHS_SUPPORT_ENABLE == SYS_FUNC_ON)
//UINT8 lang_chs[]	= {0x00,0x43, 0x00,0x68, 0x00,0x69, 0x00,0x6e, 0x00,0x65, 0x00,0x73, 0x00,0x65, 0x00,0x00};/*Chinese(Unicode)*/
UINT8 lang_chs[]	= {0x7b,0x80, 0x4f,0x53, 0x4e,0x2d, 0x65,0x87, 0x00,0x00};
#endif
#if(AOV_MALAY_SUPPORT_ENABLE == SYS_FUNC_ON)
UINT8 lang_may[]	= {0x00,0x4d, 0x00,0x61, 0x00,0x6c, 0x00,0x61, 0x00,0x79, 0x00,0x00};/*Malay(Unicode)*/
#endif
#if(AOV_IND_SUPPORT_ENABLE == SYS_FUNC_ON)
UINT8 lang_ind[]	= {0x00,0x49, 0x00,0x6e, 0x00,0x64, 0x00,0x6f, 0x00,0x6e, 0x00,0x65, 0x00,0x73, 0x00,0x69, 0x00,0x61, 0x00,0x00};/*Indonesia(Unicode)*/
#endif
#if(AOV_ARABIC_SUPPORT_ENABLE == SYS_FUNC_ON)
UINT8 lang_ara[]	= {0xfe,0xf2,0xfe,0x91,0xfe,0xae,0xfe,0xcb,0x00,0x00};/*Arabic(Unicode)*/
#endif
#if(AOV_TAMIL_SUPPORT_ENABLE == SYS_FUNC_ON)
UINT8 lang_tam[]	= {0x0b,0xa4, 0x0b,0xae, 0x0b,0xbf, 0x0b,0xb4, 0x0b,0xcd, 0x00,0x00};/*Tamil(Unicode)*/
#endif
#if(AOV_SPAN_SUPPORT_ENABLE == SYS_FUNC_ON) //Martin@20150225
UINT8 lang_span[]	= {0x00,0x65,0x00,0x73,0x00,0x70,0x00,0x61,0x00,0xf1,0x00,0x6f,0x00,0x6c,0x00,0x00};/*Spanish(Unicode)*/
#endif
#if(AOV_POR_SUPPORT_ENABLE == SYS_FUNC_ON) //Martin@20150225
UINT8 lang_por[]	= {0x00,0x50,0x00, 0x6F,0x00, 0x72,0x00, 0x74,0x00, 0x75,0x00, 0x67,0x00, 0x75,0x00, 0x65,0x00, 0x73,0x00, 0x65,0x00,0x00};/*Spanish(Unicode)*/
#endif

    UINT8* osd_lang_strs[] = 
    {
    	lang_eng,
#if(AOV_THAI_SUPPORT_ENABLE == SYS_FUNC_ON)
	lang_thai,
#endif
#if(AOV_VIE_SUPPORT_ENABLE == SYS_FUNC_ON)
	lang_vie,
#endif
#if(AOV_CHS_SUPPORT_ENABLE == SYS_FUNC_ON)
	lang_chs,
#endif
#if(AOV_MALAY_SUPPORT_ENABLE == SYS_FUNC_ON)
	lang_may,
#endif
#if(AOV_IND_SUPPORT_ENABLE == SYS_FUNC_ON)
	lang_ind,
#endif
#if(AOV_ARABIC_SUPPORT_ENABLE == SYS_FUNC_ON)
	lang_ara,
#endif
#if(AOV_TAMIL_SUPPORT_ENABLE == SYS_FUNC_ON)
	lang_tam,
#endif
#if(AOV_SPAN_SUPPORT_ENABLE == SYS_FUNC_ON)
	lang_span,
#endif
#if(AOV_POR_SUPPORT_ENABLE == SYS_FUNC_ON)
	lang_por,
#endif
    };


#define MENU_LANGUAGE_OSD_NUMBER (sizeof(osd_lang_strs)/sizeof(osd_lang_strs[0]))

#ifdef SHOW_WELCOME_SCREEN
enum
{
    LAN_ITEM1_ID = 1,
	LAN_ITEM2_ID,
	LAN_ITEM3_ID,
	LAN_ITEM4_ID,
	LAN_ITEM5_ID,
	ITEM_NEXT_ID,
	MAX_ID
};

//modify for the title of language submenu show welcome 2011 11 24
//welcome page has been solve by other method, not use the language submenu anymore
/*BOOL show_win_as_welcome = FALSE;
void set_win_lang_as_welcome(BOOL iswelcome)
{
    if (iswelcome)
        m_TitleStringId = RS_SYSTEM_WELCOME;
    else
        m_TitleStringId = RS_SYSTEM_LANGUAGE;

	show_win_as_welcome = iswelcome;
}

BOOL get_win_lang_as_welcome()
{
	return show_win_as_welcome;
}*/

#endif


UINT8 win_language_get_menu_language_osd_num()
{
	return MENU_LANGUAGE_OSD_NUMBER;
}

UINT8 win_language_get_menu_language_stream_num()
{
	return MENU_LANGUAGE_STREAM_NUMBER;
}

PRESULT comlist_menu_language_osd_callback(POBJECT_HEAD pObj, VEVENT event, UINT32 param1, UINT32 param2)
{
	UINT16 i,wTop;
	char str_len;
    	OBJLIST* ol;
	
	PRESULT cbRet = PROC_PASS;

    	ol = (OBJLIST*)pObj;
	
	if(event==EVN_PRE_DRAW)
	{
    	wTop = OSD_GetObjListTop(ol);
    	for(i=0;i<ol->wDep && (i+wTop)<ol->wCount;i++)
    	{
			win_comlist_set_str(i + wTop,NULL,osd_lang_strs[i+wTop],0 );
		}
	}
	else if(event == EVN_POST_CHANGE)
		cbRet = PROC_LEAVE;

	return cbRet;
}

PRESULT comlist_menu_language_stream_callback(POBJECT_HEAD pObj, VEVENT event, UINT32 param1, UINT32 param2)
{
	PRESULT cbRet = PROC_PASS;
	UINT16 i,wTop;
	char str_len;
	OBJLIST* ol;

	ol = (OBJLIST*)pObj;
	
	if(event==EVN_PRE_DRAW)
	{
    	wTop = OSD_GetObjListTop(ol);
    	for(i=0;i<ol->wDep && (i+wTop)<ol->wCount;i++)
    	{
			win_comlist_set_str(i + wTop,NULL,NULL,stream_lang_str_ids[i + wTop] );
		}
	}
	else if(event == EVN_POST_CHANGE)
		cbRet = PROC_LEAVE;

	return cbRet;
}

void win_lan_set_menu_lan_string(UINT8 input)
{
    UINT16 str[50];
    ComUniStrCopyChar( (UINT8 *)str, osd_lang_strs[input]);
	OSD_SetTextFieldContent(&lan_item_txtset1, STRING_UNICODE,(UINT32)str);
}

void win_lan_set_1st_audio_lan_string(UINT8 input)
{
	OSD_SetTextFieldContent(&lan_item_txtset2, STRING_ID,stream_lang_str_ids[input]);				
}

void win_lan_set_2nd_audio_lan_string(UINT8 input)
{
	OSD_SetTextFieldContent(&lan_item_txtset3, STRING_ID,stream_lang_str_ids[input]);				
}

void win_lan_set_subtitle_lan_string(UINT8 input)
{
	OSD_SetTextFieldContent(&lan_item_txtset4, STRING_ID,stream_lang_str_ids[input]);				
}

void win_lan_set_teletext_lan_string(UINT8 input)
{
	OSD_SetTextFieldContent(&lan_item_txtset5, STRING_ID,stream_lang_str_ids[input]);				
}

static VACTION language_item_win_keymap(POBJECT_HEAD pObj, UINT32 vkey)
{
    VACTION act = VACT_PASS;
    
    switch(vkey)
    {
	case V_KEY_EXIT:
	case V_KEY_MENU:
		act = VACT_CLOSE;
		break;
    }

    return act;    
}

/*******************************************************************************
*	key mapping and event callback definition
*******************************************************************************/

static VACTION lan_item_con_keymap(POBJECT_HEAD pObj, UINT32 key)
{
	VACTION act;
	
	switch(key)
	{
	case V_KEY_RIGHT:
		act = VACT_INCREASE;
		break;		
	case V_KEY_LEFT:		
		act = VACT_DECREASE;
		break;		
	case V_KEY_ENTER:
		act = VACT_ENTER;
		break;		
	default:	
		act = VACT_PASS;
		break;
	}

	return act;

}

void ap_language_setting(UINT16 audio_lang_1, UINT16 audio_lang_2) 
 {
 	sys_data_select_audio_language(audio_lang_1,audio_lang_2);
#ifndef NEW_DEMO_FRAME
	UIChChgApplyAudLanguage();
#endif
 }

void win_language_enter_key(POBJECT_HEAD pObj,UINT8 id)
{
	SYSTEM_DATA* p_sys_data=sys_data_get();
	OSD_RECT rect;
	COM_POP_LIST_PARAM_T param;
	
#ifndef SD_UI
#if Maker_GUI_ON
	rect.uTop=Maker_GUI_SM_T;
	rect.uLeft=Maker_GUI_MM_L+(Maker_GUI_SM_W/2);
	rect.uHeight=300;
	rect.uWidth=(Maker_GUI_SM_W/2);
	param.selecttype = POP_LIST_SINGLESELECT;
#else
	rect.uLeft=CON_L+TXTS_L_OF;
	rect.uWidth=TXTS_W;
	rect.uHeight=340;
	param.selecttype = POP_LIST_SINGLESELECT;
#endif	
#else
	rect.uLeft=CON_L+TXTS_L_OF;
	rect.uWidth=TXTS_W;
	rect.uHeight=250;
	param.selecttype = POP_LIST_SINGLESELECT;
#endif
	switch(id)
	{
		case MENU_LANGUAGE_MENU_LAN:
#if Maker_GUI_ON
#else
			rect.uTop=CON_T;
#endif			
			param.cur=p_sys_data->lang.OSD_lang;
			p_sys_data->lang.OSD_lang = win_com_open_sub_list(POP_LIST_MENU_LANGUAGE_OSD,&rect,&param);
			win_lan_set_menu_lan_string(p_sys_data->lang.OSD_lang);
			sys_data_select_language(p_sys_data->lang.OSD_lang);
			#ifdef BIDIRECTIONAL_OSD_STYLE
			OSD_ClearScreen();
			OSD_DrawObject((POBJECT_HEAD)&g_win_mainmenu, C_DRAW_SIGN_EVN_FLG | C_UPDATE_ALL);
			#endif
			wincom_open_title((POBJECT_HEAD)&win_lan_con,m_TitleStringId, 0);
			OSD_TrackObject((POBJECT_HEAD)&win_lan_con,C_DRAW_SIGN_EVN_FLG| C_UPDATE_ALL);
			break;
		case MENU_LANGUAGE_1ST_AUDIO_LAN:
#if Maker_GUI_ON
#else		
			rect.uTop=CON_T + (CON_H + CON_GAP)*1;
#endif			
			param.cur=p_sys_data->lang.Audio_lang_1;
			p_sys_data->lang.Audio_lang_1 = win_com_open_sub_list(POP_LIST_MENU_LANGUAGE_STREAM,&rect,&param);
			win_lan_set_1st_audio_lan_string(p_sys_data->lang.Audio_lang_1);
			OSD_TrackObject(pObj,C_UPDATE_ALL);
			ap_language_setting(p_sys_data->lang.Audio_lang_1, p_sys_data->lang.Audio_lang_2);
			break;
		case MENU_LANGUAGE_2ND_AUDIO_LAN:
#if Maker_GUI_ON
#else		
			rect.uTop=CON_T + (CON_H + CON_GAP)*2;
#endif			
			param.cur=p_sys_data->lang.Audio_lang_2;
			p_sys_data->lang.Audio_lang_2 = win_com_open_sub_list(POP_LIST_MENU_LANGUAGE_STREAM,&rect,&param);
			win_lan_set_2nd_audio_lan_string(p_sys_data->lang.Audio_lang_2);
			OSD_TrackObject(pObj,C_UPDATE_ALL);
			ap_language_setting(p_sys_data->lang.Audio_lang_1, p_sys_data->lang.Audio_lang_2);
			break;
		case MENU_LANGUAGE_SUB_LAN:
#if Maker_GUI_ON
#else		
			rect.uTop=CON_T + (CON_H + CON_GAP)*3;
#endif			
			param.cur=p_sys_data->lang.sub_lang;
			p_sys_data->lang.sub_lang = win_com_open_sub_list(POP_LIST_MENU_LANGUAGE_STREAM,&rect,&param);
			win_lan_set_subtitle_lan_string(p_sys_data->lang.sub_lang);
			OSD_TrackObject(pObj,C_UPDATE_ALL);
			break;
		case MENU_LANGUAGE_TXT_LAN:
#if Maker_GUI_ON
#else		
			rect.uTop=CON_T + (CON_H + CON_GAP)*3;
			#ifndef SD_UI
			rect.uHeight=300;
			#else
			rect.uHeight=200;
			#endif
#endif			
			param.cur=p_sys_data->lang.ttx_lang;
			p_sys_data->lang.ttx_lang = win_com_open_sub_list(POP_LIST_MENU_LANGUAGE_STREAM,&rect,&param);
			win_lan_set_teletext_lan_string(p_sys_data->lang.ttx_lang);
			OSD_TrackObject(pObj,C_UPDATE_ALL);
			break;
	}
}

void win_language_left_key(POBJECT_HEAD pObj,UINT8 id)
{
	SYSTEM_DATA* p_sys_data=sys_data_get();
	switch(id)
	{
		case MENU_LANGUAGE_MENU_LAN:
			if(p_sys_data->lang.OSD_lang==0)
			{
				p_sys_data->lang.OSD_lang=MENU_LANGUAGE_OSD_NUMBER-1;
			}
			else
			{
				p_sys_data->lang.OSD_lang--;
			}
			win_lan_set_menu_lan_string(p_sys_data->lang.OSD_lang);
			sys_data_select_language(p_sys_data->lang.OSD_lang);
			#ifdef BIDIRECTIONAL_OSD_STYLE
			OSD_ClearScreen();
			OSD_DrawObject((POBJECT_HEAD)&g_win_mainmenu, C_DRAW_SIGN_EVN_FLG | C_UPDATE_ALL);
			#endif
			wincom_open_title((POBJECT_HEAD)&win_lan_con,m_TitleStringId, 0);
			OSD_TrackObject((POBJECT_HEAD)&win_lan_con,C_DRAW_SIGN_EVN_FLG| C_UPDATE_ALL);
			break;
		case MENU_LANGUAGE_1ST_AUDIO_LAN:
			if(p_sys_data->lang.Audio_lang_1==0)
			{
				p_sys_data->lang.Audio_lang_1=MENU_LANGUAGE_STREAM_NUMBER-1;
			}
			else
			{
				p_sys_data->lang.Audio_lang_1--;
			}
			win_lan_set_1st_audio_lan_string(p_sys_data->lang.Audio_lang_1);
			OSD_TrackObject(pObj,C_UPDATE_ALL);
			ap_language_setting(p_sys_data->lang.Audio_lang_1, p_sys_data->lang.Audio_lang_2);
			break;
		case MENU_LANGUAGE_2ND_AUDIO_LAN:
			if(p_sys_data->lang.Audio_lang_2==0)
			{
				p_sys_data->lang.Audio_lang_2=MENU_LANGUAGE_STREAM_NUMBER-1;
			}
			else
			{
				p_sys_data->lang.Audio_lang_2--;
			}
			win_lan_set_2nd_audio_lan_string(p_sys_data->lang.Audio_lang_2);
			OSD_TrackObject(pObj,C_UPDATE_ALL);
			ap_language_setting(p_sys_data->lang.Audio_lang_1, p_sys_data->lang.Audio_lang_2);
			break;
		case MENU_LANGUAGE_SUB_LAN:
			if(p_sys_data->lang.sub_lang==0)
			{
				p_sys_data->lang.sub_lang=MENU_LANGUAGE_STREAM_NUMBER-1;
			}
			else
			{
				p_sys_data->lang.sub_lang--;
			}
			win_lan_set_subtitle_lan_string(p_sys_data->lang.sub_lang);
			OSD_TrackObject(pObj,C_UPDATE_ALL);
			break;
		case MENU_LANGUAGE_TXT_LAN:
			if(p_sys_data->lang.ttx_lang==0)
			{
				p_sys_data->lang.ttx_lang=MENU_LANGUAGE_STREAM_NUMBER-1;
			}
			else
			{
				p_sys_data->lang.ttx_lang--;
			}
			win_lan_set_teletext_lan_string(p_sys_data->lang.ttx_lang);
			OSD_TrackObject(pObj,C_UPDATE_ALL);
			break;
	}
}

void win_language_right_key(POBJECT_HEAD pObj,UINT8 id)
{
	SYSTEM_DATA* p_sys_data=sys_data_get();
	switch(id)
	{
		case MENU_LANGUAGE_MENU_LAN:
			if(p_sys_data->lang.OSD_lang==(MENU_LANGUAGE_OSD_NUMBER-1))
			{
				p_sys_data->lang.OSD_lang=0;
			}
			else
			{
				p_sys_data->lang.OSD_lang++;
			}
			win_lan_set_menu_lan_string(p_sys_data->lang.OSD_lang);
			sys_data_select_language(p_sys_data->lang.OSD_lang);
			#ifdef BIDIRECTIONAL_OSD_STYLE
			OSD_ClearScreen();
			OSD_DrawObject((POBJECT_HEAD)&g_win_mainmenu, C_DRAW_SIGN_EVN_FLG | C_UPDATE_ALL);
			#endif
			wincom_open_title((POBJECT_HEAD)&win_lan_con,m_TitleStringId, 0);
			OSD_TrackObject((POBJECT_HEAD)&win_lan_con,C_DRAW_SIGN_EVN_FLG| C_UPDATE_ALL);
			break;
		case MENU_LANGUAGE_1ST_AUDIO_LAN:
			if(p_sys_data->lang.Audio_lang_1==(MENU_LANGUAGE_STREAM_NUMBER-1))
			{
				p_sys_data->lang.Audio_lang_1=0;
			}
			else
			{
				p_sys_data->lang.Audio_lang_1++;
			}
			win_lan_set_1st_audio_lan_string(p_sys_data->lang.Audio_lang_1);
			OSD_TrackObject(pObj,C_UPDATE_ALL);
			ap_language_setting(p_sys_data->lang.Audio_lang_1, p_sys_data->lang.Audio_lang_2);
			break;
		case MENU_LANGUAGE_2ND_AUDIO_LAN:
			if(p_sys_data->lang.Audio_lang_2==(MENU_LANGUAGE_STREAM_NUMBER-1))
			{
				p_sys_data->lang.Audio_lang_2=0;
			}
			else
			{
				p_sys_data->lang.Audio_lang_2++;
			}
			win_lan_set_2nd_audio_lan_string(p_sys_data->lang.Audio_lang_2);
			OSD_TrackObject(pObj,C_UPDATE_ALL);
			ap_language_setting(p_sys_data->lang.Audio_lang_1, p_sys_data->lang.Audio_lang_2);
			break;
		case MENU_LANGUAGE_SUB_LAN:
			if(p_sys_data->lang.sub_lang==(MENU_LANGUAGE_STREAM_NUMBER-1))
			{
				p_sys_data->lang.sub_lang=0;
			}
			else
			{
				p_sys_data->lang.sub_lang++;
			}
			win_lan_set_subtitle_lan_string(p_sys_data->lang.sub_lang);
			OSD_TrackObject(pObj,C_UPDATE_ALL);
			break;
		case MENU_LANGUAGE_TXT_LAN:
			if(p_sys_data->lang.ttx_lang==(MENU_LANGUAGE_STREAM_NUMBER-1))
			{
				p_sys_data->lang.ttx_lang=0;
			}
			else
			{
				p_sys_data->lang.ttx_lang++;
			}
			win_lan_set_teletext_lan_string(p_sys_data->lang.ttx_lang);
			OSD_TrackObject(pObj,C_UPDATE_ALL);
			break;
	}
}

static PRESULT lan_item_con_callback(POBJECT_HEAD pObj, VEVENT event, UINT32 param1, UINT32 param2)
{
	PRESULT ret = PROC_PASS;
	UINT8	unact,input;
	UINT8	id = OSD_GetObjID(pObj);
	POBJECT_HEAD submenu;
	SYSTEM_DATA* p_sys_data=sys_data_get();
	UINT8 lang_code[8];

	switch(event)
	{		
	case EVN_FOCUS_PRE_GET:
		break;
		
	case EVN_FOCUS_PRE_LOSE:
		break;

	case EVN_UNKNOWN_ACTION:
		unact = (VACTION)(param1>>16) ;
#ifdef SHOW_WELCOME_SCREEN
		if(id != ITEM_NEXT_ID)
		{
#endif
			if(unact == VACT_ENTER)
			{
				win_language_enter_key(pObj,id);
			}
			else if(unact == VACT_DECREASE)
			{
				win_language_left_key(pObj,id);
			}
			else if(unact == VACT_INCREASE)
			{
				win_language_right_key(pObj,id);
			}

#ifdef SHOW_WELCOME_SCREEN
		}
		else if(unact == VACT_ENTER)
		{
    #ifdef EPG_MULTI_LANG
			/*set epg language*/
			p_sys_data = sys_data_get();
			STRCPY(lang_code,stream_iso_639lang_abbr[p_sys_data->lang.OSD_lang]);
			epg_set_lang_code(lang_code);
    #endif

    #ifndef NEW_DEMO_FRAME
			UIChChgApplyAudLanguage();
    #endif
			sys_data_save(1);
       	 	TTXEng_set_g0_set();
			//show_win_as_welcome = FALSE;
			submenu = (POBJECT_HEAD) & win_country_net_con;
			if (OSD_ObjOpen(submenu, 0xFFFFFFFF) != PROC_LEAVE)
			{
				menu_stack_pop();
				menu_stack_push(submenu);
			}
		}
#endif
		ret = PROC_LOOP;
		break;
	}
	return ret;

}

static VACTION lan_con_keymap(POBJECT_HEAD pObj, UINT32 key)
{
	VACTION act;
	
	switch(key)
	{
	case V_KEY_UP:
		act = VACT_CURSOR_UP;
		break;
		
	case V_KEY_DOWN:
		act = VACT_CURSOR_DOWN;
		break;		
		
	case V_KEY_EXIT:
	case V_KEY_MENU:
//modify for the title of language submenu show welcome
//welcome page has been solve by other method, not use the language submenu anymore
/*#ifdef SHOW_WELCOME_SCREEN
		if(show_win_as_welcome)
			act = VACT_PASS;
		else
#endif*/
		act = VACT_CLOSE;
		break;
		
	case V_KEY_ENTER:
		act = VACT_ENTER;
		break;

	default:	
		act = VACT_PASS;
		break;
	}

	return act;
}

extern char *stream_iso_639lang_abbr[];
static PRESULT lan_con_callback(POBJECT_HEAD pObj, VEVENT event, UINT32 param1, UINT32 param2)
{
	PRESULT ret = PROC_PASS;
	UINT32 i;
	UINT8	unact;
	BOOL subt_popup;
	CONTAINER* cont = (CONTAINER*)pObj;
	UINT8	id = OSD_GetObjID(pObj);
	SYSTEM_DATA* p_sys_data;
	UINT8 lang_code[8];
    
	switch(event)
	{
	case EVN_PRE_OPEN:
		wincom_open_title((POBJECT_HEAD)&win_lan_con,m_TitleStringId, 0);
#ifdef SHOW_WELCOME_SCREEN
//modify for the title of language submenu show welcome
//welcome page has been solve by other method, not use the language submenu anymore
		//if(show_win_as_welcome)
		if(1)
		{
			OSD_SetAttr(&lan_item_con6, C_ATTR_ACTIVE);
			((POBJECT_HEAD)(&lan_item_con1))->bUpID = 6;
			((POBJECT_HEAD)(&lan_item_con5))->bDownID = 6;
            win_lan_con.head.style.bShowIdx = WSTL_WIN_BODYLEFT_01_HD;
            win_lan_con.head.style.bHLIdx = WSTL_WIN_BODYLEFT_01_HD;
            win_lan_con.head.style.bSelIdx = WSTL_WIN_BODYLEFT_01_HD;
            win_lan_con.head.style.bGrayIdx = WSTL_WIN_BODYLEFT_01_HD;
            OSD_SetRect(&(win_lan_con.head.frame), MAIN_W_L, W_T, MAIN_W_W, W_H);
		}
		else
		{
			OSD_SetAttr(&lan_item_con6, C_ATTR_HIDDEN);
			((POBJECT_HEAD)(&lan_item_con1))->bUpID = 5;
			((POBJECT_HEAD)(&lan_item_con5))->bDownID = 1;
            win_lan_con.head.style.bShowIdx = WIN_SH_IDX;
            win_lan_con.head.style.bHLIdx = WIN_HL_IDX;
            win_lan_con.head.style.bSelIdx = WIN_SL_IDX;
            win_lan_con.head.style.bGrayIdx = WIN_GRY_IDX;

            OSD_SetRect(&(win_lan_con.head.frame), W_L, W_T, W_W, W_H);
    		//remove_menu_item(&win_lan_con, (OBJECT_HEAD *)&lan_item_con6, CON_H + CON_GAP);
			
		}
         ((CONTAINER*)(&win_lan_con))->FocusObjectID = 1;
#endif

		p_sys_data=sys_data_get();
		win_lan_set_menu_lan_string(p_sys_data->lang.OSD_lang);
		win_lan_set_1st_audio_lan_string(p_sys_data->lang.Audio_lang_1);
		win_lan_set_2nd_audio_lan_string(p_sys_data->lang.Audio_lang_2);
		win_lan_set_subtitle_lan_string(p_sys_data->lang.sub_lang);
		win_lan_set_teletext_lan_string(p_sys_data->lang.ttx_lang);
#ifdef SUPPORT_POP_SUBT
		subt_popup = TRUE;
#else
		subt_popup = FALSE;
#endif
//Gavim@menu
		if(subt_popup)
		{
			//remove_menu_item(&win_lan_con, (POBJECT_HEAD)lan_items[3], (CON_H + CON_GAP));	
			remove_menu_item(&win_lan_con, (POBJECT_HEAD)lan_items[5], (CON_H + CON_GAP)); //Martin@20150206			
		}
		break;
	
	case EVN_POST_OPEN:
		break;
		
	case EVN_PRE_CLOSE:
#ifdef EPG_MULTI_LANG		
		/*set epg language*/
		p_sys_data = sys_data_get();
		STRCPY(lang_code,stream_iso_639lang_abbr[p_sys_data->lang.OSD_lang]);
		epg_set_lang_code(lang_code);
#endif
		
#ifndef NEW_DEMO_FRAME
		UIChChgApplyAudLanguage();
#endif
		sys_data_save(1);
		/* Make OSD not flickering */
		*((UINT32*)param2) &= ~C_CLOSE_CLRBACK_FLG;	
		
		break;

	case EVN_POST_CLOSE:
	#if	(TTX_ON == 1)		
        TTXEng_set_g0_set();
	#endif
		break;
	}
	return ret;
}

